package com.atguigu.srb.util;

import com.atguigu.srb.exception.SRBException;
import com.atguigu.srb.result.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

/**
 * ClassName: SrbAssert
 * Package: com.atguigu.srb.util
 * Description:
 *
 * @Author 刘强
 * @Create 2024/4/15 21:02
 * @Version 1.0
 */
// 断言：以简洁的语法，要求数据必须满足指定的条件，否则报错
@Slf4j
public class SrbAssert {
    public static Boolean assertNotNull(Object obj){
        if(obj == null){
            SRBException srbException = new SRBException(ResponseEnum.NOT_NULL);
            log.error(ExceptionUtils.getStackTrace(srbException));
            throw srbException;
        }
        return true;
    }
    public static Boolean assertNotNull(Object obj,ResponseEnum responseEnum){
        if(obj == null){
            SRBException srbException = new SRBException(responseEnum.getMessage(),responseEnum.getCode());
            log.error(ExceptionUtils.getStackTrace(srbException));
            throw srbException;
        }
        return true;
    }

    public static Boolean assertNull(Object obj){
        if(obj != null){
            SRBException srbException = new SRBException(ResponseEnum.NOT_NULL);
            log.error(ExceptionUtils.getStackTrace(srbException));
            throw srbException;
        }
        return true;
    }
    public static Boolean assertNull(Object obj,ResponseEnum responseEnum){
        if(obj != null){
            SRBException srbException = new SRBException(responseEnum.getMessage(),responseEnum.getCode());
            log.error(ExceptionUtils.getStackTrace(srbException));
            throw srbException;
        }
        return true;
    }

    public static void assertNotTrue(Boolean flag) {
        if(flag){
            SRBException srbException = new SRBException(ResponseEnum.HAS_CHILDREN_ERROR);
            log.error(ExceptionUtils.getStackTrace(srbException));
            throw srbException;
        }
    }
    public static void assertNotTrue(Boolean flag,ResponseEnum responseEnum) {
        if(flag){
            SRBException srbException = new SRBException(responseEnum.getMessage(),responseEnum.getCode());
            log.error(ExceptionUtils.getStackTrace(srbException));
            throw srbException;
        }
    }

    public static void assertTrue(Boolean flag) {
        if(!flag){
            SRBException srbException = new SRBException("必须为true", -111);
            log.error(ExceptionUtils.getStackTrace(srbException));
        }
    }
    public static void assertTrue(Boolean flag,ResponseEnum responseEnum) {
        if(!flag){
            SRBException srbException = new SRBException(responseEnum.getMessage(),responseEnum.getCode());
            log.error(ExceptionUtils.getStackTrace(srbException));
            throw srbException;
        }
    }
}
